define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: "ID", sortable: true, operate: false },
                        { field: 'unique_id', title: "用户分享码", operate: 'LIKE' },
                        // {field: 'group.name', title: __('Group')},
                        //                        { field: 'user', title: "用户", searchable: false, formatter: Controller.api.formatter.user, },
                        //                        { field: 'username', title: "用户帐号", visible:true, operate: 'LIKE', },
                        { field: 'nickname', title: "用户昵称", visible: true, operate: 'LIKE', },
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        { field: 'mobile', title: "用户手机", operate: 'LIKE' },
                        { field: 'avatar', title: "用户头像", searchable: false, events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },

                        { field: 'parent_user_unique_id', title: "推荐人分享码", searchable: false },
                        { field: 'invite.unique_id', title: "推荐人分享码", visible: false, operate: 'LIKE', },
                        { field: 'parent_user_mobile', title: "推荐人手机", searchable: false },
                        { field: 'invite.mobile', title: "推荐人手机", visible: false, operate: 'LIKE', },
                       //1：团长，2：家庭酒庄，3：区县代理商
                        { field: 'expert_level', title: "级别", searchList: { 0: '其他',1: '团长', 2: '家庭酒庄', 3: '区县代理商' },formatter:Table.api.formatter.normal},
                        { field: 'logintime', title: "登陆时间", formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        { field: 'loginip', title: "登陆IP", formatter: Table.api.formatter.search },
                        { field: 'jointime', title: "加入时间", formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        // { field: 'joinip', title: "加入IP", formatter: Table.api.formatter.search },
                        { field: 'status', title: "用户状态", formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        { field: 'operate', title: "操作", table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ],
                exportTypes: ['csv', 'excel'],//设置导出格式
                exportOptions: {
                    fileName: 'export_' + Moment().format("YYYY-MM-DD"),  //导出表格名称
                    ignoreColumn: ['avatar', 'operate'], //不导出字段
                },
                // commonSearch: false,
                visible: false,
                showToggle: false,
                showColumns: false,
                showExport: true,
            });

            //给添加按钮添加`data-area`属性
            $(".btn-add").data("area", ["70%", "70%"]);

            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit").data("area", ["70%", "70%"]);
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                //区经理商身份变换，显示省市区
                $(document).on("change", "#id_expert_level", function(data){
                    //变更后的回调事件
                    var value = $(this).val();
                    if (value == 3) {
                        $("#is_area_manager_group").removeClass('hidden');
                    } else {
                        $("#is_area_manager_group").addClass('hidden');
                    }
                });
            },
            formatter: {
                yes_or_no: function (value, row, index) {
                    if (value == "1") {
                        return "是"
                    } else {
                        return "否"
                    }
                },
                user: function (value, row, index) {
                    var html = row["nickname"] + "<br/>" + row["mobile"];
                    return html;
                },
                expert_level: function (value, row, index) {
                    var expert_level = "";
                    if (value == "0") {
                        expert_level = "----";
                    }
                    if (value == "1") {
                        expert_level = "一星达人";
                    }
                    if (value == "2") {
                        expert_level = "二星达人";
                    }
                    if (value == "3") {
                        expert_level = "三星达人";
                    }
                    return expert_level;
                },
                marketing_level: function (value, row, index) {
                    var marketing_level = "";
                    if (row.marketing_level == "1") {
                        marketing_level = "一星董事";
                    }
                    if (row.marketing_level == "2") {
                        marketing_level = "二星董事";
                    }
                    if (row.marketing_level == "3") {
                        marketing_level = "三星董事";
                    }
                    return marketing_level;
                },
                parent_user: function (value, row, index) {
                    var parent_user_str = "";
                    if (row.parent_user_mobile != null && row.parent_user_mobile != "") {
                        parent_user_str = row.parent_user_mobile;
                    }
                    return parent_user_str;
                },
            }
        }
    };
    return Controller;
});